﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZhangZhao.WebAPI
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = Output.Fail;

            try
            {
                string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
                string Password = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Password]);
                string Email = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Email]);

                if (!string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password) && !string.IsNullOrWhiteSpace(Email))
                {
                    ZhangZhao.BLL.H_User bll = new ZhangZhao.BLL.H_User();

                    if (bll.Exists(UserName))
                    {
                        result = Output.UserNameExist + ":用户名已存在";
                    }
                    else if (bll.EmailExists(Email))
                    {
                        result = Output.EmailExist + ":Email已存在";
                    }
                    else
                    {
                        int id = bll.Add(new ZhangZhao.Model.H_User()
                        {
                            CreatedTime = DateTime.Now,
                            IsEffective = true,
                            Password = Password,
                            UserName = UserName
                        });

                        if (id > 0)
                        {
                            ZhangZhao.BLL.H_UserContact biz = new ZhangZhao.BLL.H_UserContact();
                            biz.Add(new ZhangZhao.Model.H_UserContact()
                            {
                                UserName = UserName,
                                Email = Email
                            });
                        }

                        result = Output.Success;
                    }
                }
            }
            catch (Exception ex)
            {
                result = Output.Fail + ":" + ex.Message;
            }

            Response.Clear();
            Response.Write(result);
            Response.End();
        }
    }
}